package com.shd.edanpei.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.adapter.MeMsgAdapter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.presenter.contract.MeMessageContract;
import com.shd.edanpei.presenter.MeMessagePresenter;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/11/6.
 * yang-wen-wen
 */

public class MeMessageActivity extends BaseActivity<MeMessagePresenter>
        implements MeMessageContract.MeMessageIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private MeMsgAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_me_message;
    }

    @Override
    protected MeMessagePresenter bindPresenter() {
        return new MeMessagePresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        headline.setText("平台消息");
        mAdapter = new MeMsgAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        mPresenter.refresh();
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh();
            }
        });
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                switch (mAdapter.getItem(position).getModelType()) {
//                    case 1:
//                        //订单
//                        Bundle bundle = new Bundle();
//                        bundle.putInt(Constant.ORDER_ID, mAdapter.getItem(position).getOrderId());
//                        bundle.putInt(Constant.TYPE, mAdapter.getData().get(position).getOrderType());
//                        bundle.putInt(Constant.INDEX, -1);
//                        startActivity(MeOrderDetailsActivity.class, bundle);
//                        break;
//                    case 2:
//                        //积分
//                        startActivity(AccountActivity.class);
//                        break;
//                    case 3:
//                        //新订单
//                        RxBus.get().send(RxBusCode.TO_ShareOrderFragment);
//                        finish();
//                        break;
//                    default:
//                        break;
//                }
//
//                getMessage(mAdapter.getItem(position).getId(), position);
//            }
//        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadMore();
            }
        }, recyclerView);
    }

    @OnClick(R.id.iv_headline_back)
    void onBack() {
        finish();
    }

    private void getMessage(final int id, final int index) {
        ApiCreator.getApi().messageDetails(id)
                .compose(RxTransformer.<MessageModel>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MessageModel>() {
                    @Override
                    protected void onSuccess(MessageModel data) {
//                        mAdapter.getItem(index).setStatus(0);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    @Override
    public MeMsgAdapter getAdapter() {
        return mAdapter;
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }
}
